/**      
 * @file		Palindrome.cpp
 * @description		This sample uses recursion to determine if strings
 *			are palindromes. A palindrome is a string that is
 *			exactly the same written forward or in reverse.
 * @course		CSCI 123 Section 00000
 * @assignment 
 * @date		mm/dd/yyyy
 * @author		Brad Rippe (00000000) brippe@fullcoll.edu
 * @version		1.0
 */
#include <iostream>
#include <cstring>
using namespace std;

/**
 * Creates a new cstring from a subsection of a string.
 * @param aStr the original string the subsection is taken from.
 * @param aStart the index of the first character
 * @param aNumOfChars the number of characters to add to the new 
 *		string starting from the character at aStart
 * @return the new string created from the specified subsection
 */
char* substring(const char* aStr, int aStart, int aNumOfChars);

/**
 * Determines if a string is a palindrome.
 * @param aString the string to be checked.
 * @return true if the string is a palindrome, otherwise false.
 */
bool isPalindrome(const char* const aString);

/**
 * @return zero if the application executes successfully
 */
int main() {

	char string1[] = "civic";
	cout << "Testing civic to see if its a palindrome.\n";
	cout << "Is it a palindrome ";
	if(isPalindrome(string1))
		cout << "yes\n";
	else
		cout << "no\n";

	char string2[] = "level";
	cout << "Testing level to see if its a palindrome.\n";
	cout << "Is it a palindrome ";
	if(isPalindrome(string2))
		cout << "yes\n";
	else
		cout << "no\n";

	return 0;
}

bool isPalindrome(const char* const aString) {
	if(strlen(aString) <= 1) {
		return true;
	} else if(aString[0] != aString[strlen(aString)-1]) {
		return false;
	} else {
		return isPalindrome(substring(aString, 1, strlen(aString)-2));
	}
}

char* substring(const char* aStr, int aStart, int aNumOfChars) {
	char *newString = new char[aNumOfChars];
	int count = 0;
	for(int i = aStart; i <= aNumOfChars; i++)
		newString[count++] = aStr[i];
	newString[count] = '\0';
	return newString;
}
